package com.zzyl.serve.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import com.zzyl.serve.service.INursingPlanService;
import com.zzyl.serve.vo.NursingLevelVo;
import com.zzyl.serve.vo.NursingPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.zzyl.common.constant.CacheConstants.NURSING_LEVEL_KEY;

/**
 * 护理等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private INursingPlanService iNursingPlanService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        NursingLevel nursingLevel = nursingLevelMapper.selectNursingLevelById(id);
        return nursingLevel;
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelList(NursingLevel nursingLevel) {
        //查询所有护理等级的基本字段
        List<NursingLevel> nursingLevels = nursingLevelMapper.selectNursingLevelList(nursingLevel);

        //遍历所有护理等级数据，把数据拷贝给新的vo对象中。
        List<NursingLevelVo> collect = nursingLevels.stream().map(n -> {
            NursingLevelVo nursingLevelVo = new NursingLevelVo();
            BeanUtils.copyProperties(n, nursingLevelVo);
            //查询护理计划当中所有的数据
            NursingPlanVo nursingPlanVo = iNursingPlanService.selectNursingPlanById(n.getLplanId());
            String planName = nursingPlanVo.getPlanName();
            //给护理等级当中的planName赋值
            nursingLevelVo.setPlanName(planName);
            return nursingLevelVo;
        }).collect(Collectors.toList());

        return collect;
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        int flag = nursingLevelMapper.insertNursingLevel(nursingLevel);
        // 删除缓存
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return flag;
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        int flag = nursingLevelMapper.updateNursingLevel(nursingLevel);
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return flag;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        int flag = nursingLevelMapper.deleteNursingLevelByIds(ids);
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return flag;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        int flag = nursingLevelMapper.deleteNursingLevelById(id);
        redisTemplate.delete(NURSING_LEVEL_KEY);
        return flag;
    }

    /**
     * 查询所有护理等级(Day6-课堂5)
     *
     * @return
     */
    @Override
    public List<NursingLevel> getAllLevels() {
        //先查询缓存是否存在
        List<NursingLevel> nursingLevelList = (List<NursingLevel>) redisTemplate.opsForValue().get(NURSING_LEVEL_KEY);

        if (ObjectUtil.isNotEmpty(nursingLevelList)) {
            //如果存在则直接返回，
            return nursingLevelList;
        }
        //不存在则查询数据库，并把数据保存到缓存中。
        nursingLevelList = this.lambdaQuery().eq(NursingLevel::getStatus, 1).list();
        redisTemplate.opsForValue().set(NURSING_LEVEL_KEY, nursingLevelList);
        return nursingLevelList;
    }
}
